package com.franzliszt.maillist.atcivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.franzliszt.maillist.R;
import com.franzliszt.maillist.sql.Dao;
import com.franzliszt.maillist.sql.News;
import com.franzliszt.maillist.util.ChatExchange;
import com.franzliszt.maillist.util.ContactsInfo;
import com.franzliszt.maillist.util.KillProcess;

import java.util.ArrayList;
import java.util.List;

public class IncreaseActivity extends AppCompatActivity {
    private TextView Complete;
    private List<News> newsList = new ArrayList<>();
    private Dao dao;
    private EditText LastName,FirstName,PhoneNumber,School,Address,Other;
    private Toast Error;
    private TextView Tips;
    private KillProcess killProcess;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE );
            getWindow().setStatusBarColor( Color.TRANSPARENT );
        }
        setContentView(R.layout.activity_increase);
        InitView();
    }
    private void InitView(){
        Complete = findViewById(R.id.AddContacts);
        Complete.setText("完成");

        LastName = findViewById(R.id.LastName);
        FirstName = findViewById(R.id.FirstName);
        PhoneNumber = findViewById( R.id.PhoneNumber);
        School = findViewById(R.id.School);
        Address = findViewById(R.id.Address);
        Other = findViewById(R.id.Other);

        Error = new Toast(IncreaseActivity.this);
        View view = LayoutInflater.from( IncreaseActivity.this ).inflate( R.layout.passworderror,null,false );
        Tips = view.findViewById(R.id.ErrorTips);
        Error.setView( view );
        Error.setGravity( Gravity.CENTER,0,0 );
        dao = new Dao(IncreaseActivity.this);
        try {
            newsList = dao.QueryAll();
        }catch (NullPointerException e){
            e.printStackTrace();
        }

        killProcess = KillProcess.getInstance();
        killProcess.PushActivity(IncreaseActivity.this);

    }
    public void OtherThings(View view){
        AddContacts();
    }
    public void Exit(View view){
        startActivity(new Intent(IncreaseActivity.this,ContactsActivity.class));
    }
    private void AddContacts(){
        String lastname = LastName.getText().toString();
        String firstname = FirstName.getText().toString();
        String pinyin = ChatExchange.getInstance().getPinyin(lastname+firstname);
        String phone = PhoneNumber.getText().toString();
        String school = School.getText().toString();
        String addr = Address.getText().toString();
        String other = Other.getText().toString();
        if (TextUtils.isEmpty(lastname)){
            Tips.setText("姓氏不能为空");
            Error.show();
            return;
        }
        if (TextUtils.isEmpty(firstname)){
            Tips.setText("名字不能为空");
            Error.show();
            return;
        }
        if (TextUtils.isEmpty(phone)){
            Tips.setText("手机号码不能为空");
            Error.show();
            return;
        }
        if (newsList != null){
            for (int i = 0; i <newsList.size() ; i++) {
                if (phone.equals(newsList.get(i).getPhoneNumber())){
                    Tips.setText("该手机号码已被绑定");
                    return;
                }
            }
        }
        News news = new News(lastname,firstname,pinyin,phone,school == null ? " ":school,addr == null ? " ":addr,other == null ? " ":other);
        dao.Insert(news);
        Tips.setText("插入成功");
        Error.show();
    }
}
